//
//  NISearchBar.m
//  VWCarNet
//
//  Created by navinfo air on 16/9/1.
//  Copyright © 2016年 navinfo. All rights reserved.
//

#import "NISearchBar.h"

@implementation NISearchBar{
    
    __weak IBOutlet UIView *inputBg;
    __weak IBOutlet UITextField *textField;
    __weak IBOutlet UILabel *placeholderLabel;
    __weak IBOutlet UIButton *cancelButton;
    __weak IBOutlet NSLayoutConstraint *cancelButtonWidthConstraint;
    __weak IBOutlet UIView *bgView;
    
    __weak IBOutlet UIButton *clearButton;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:self.contentView];
    
    inputBg.layer.masksToBounds = YES;
    inputBg.layer.cornerRadius = 6.0f;
    
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
    
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    textField.limitMaxTextLength = 255;
    cancelButtonWidthConstraint.constant = 0;
    cancelButton.hidden = YES;
    clearButton.hidden = YES;
    placeholderLabel.textColor = [UIColor redColor];
    [cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:17];
}

- (void)setBackgroundColor:(UIColor *)backgroundColor{
    [super setBackgroundColor:backgroundColor];
    if ([backgroundColor isEqual:[UIColor clearColor]]) {
        self.contentView.backgroundColor = backgroundColor;
        bgView.backgroundColor = backgroundColor;
        
    }
}

- (void)setPlaceholder:(NSString *)text{
    placeholderLabel.text = text;
}

-(void)clearText
{
    textField.text = @"";
    placeholderLabel.hidden = NO;
    clearButton.hidden = YES;
    [self searchStrChange];
}

-(void)setText:(NSString *)text
{
    textField.text = text;
    if ([text isEqualToString:@""]) {
        
        placeholderLabel.hidden = NO;
        clearButton.hidden = YES;
    }
    else
    {
        
        placeholderLabel.hidden = YES;
        clearButton.hidden = NO;
    }
    [self searchStrChange];
}

- (IBAction)onClearButtonClicked:(id)sender {
    textField.text = @"";
    [textField becomeFirstResponder];
    placeholderLabel.hidden = NO;
    clearButton.hidden = YES;
    [self searchStrChange];
    if (self.delegate) {
        [self.delegate onSearchClear];
    }
}
- (IBAction)onCancelButtonClicked:(id)sender {
    [textField resignFirstResponder];
    if (self.delegate) {
        [self.delegate onSearchCancel];
    }
    [self searchStrChange];
}

- (NSString *)getInputText{
    return textField.text;
}

-(void)searchStrChange
{
    if (self.delegate ) {
        [self.delegate onSearchChange];
    }
}

-(void)hiddenKeyboard
{
    [textField resignFirstResponder];
}

-(void)showKeyboard
{
    [textField becomeFirstResponder];
}

-(void)showCancelBtnWithTitle:(NSString *)title
{
    [cancelButton setTitle:title forState:UIControlStateNormal];
    cancelButtonWidthConstraint.constant = 60;
    cancelButton.hidden = NO;
}

-(void)hiddenCancelBtn
{
    cancelButtonWidthConstraint.constant = 0;
    cancelButton.hidden = YES;
}

#pragma - mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField{
    [self.delegate onSearch:aTextField.text];
    return YES;
}

- (void)textFieldDidChange:(UITextField *)aTextField{
    if (![textField.text isEqualToString:@""]) {
        placeholderLabel.hidden = YES;
        clearButton.hidden = NO;
    }else{
        placeholderLabel.hidden = NO;
        clearButton.hidden = YES;
    }
    [self searchStrChange];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [self.delegate onBeginEditing];
    if (_disableEditing) {
        return NO;
    }
    return YES;
}

@end
